﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Collections;

using XJX.DBCore;

namespace XJX.DataLayer
{
    public sealed class Exchange : XJX.DBCore.DBObject
    {
        public Exchange(string newConnectionString) : base(newConnectionString) { }

        public bool Add_New_ExchangeTheme
            (
            string Title,
            string Content,
            string UID,
            out int ReturnValue,          //返回参数值
            out string ReturnFieldValue   //返回字段值,返回插入的新值ID编号
            )
        {
            int rowsAffected;//受影响的行数．

            SqlParameter[]
                parameters = {
								 new SqlParameter("@title",SqlDbType.VarChar,500), 
								 new SqlParameter("@content",SqlDbType.VarChar,-1),
								 new SqlParameter("@uid",SqlDbType.UniqueIdentifier)
			};

            parameters[0].Value = Title;
            parameters[1].Value = Content;
            parameters[2].Value = new Guid(UID);

            try
            {
                //以下语句可以得到三个值，从而进行流程判断。一个是返回值，一个是影响的行数，一个是返回的指定字段值。
                ReturnValue = RunProcedure("ET_Add_New_ExchangeTheme", parameters, out rowsAffected, out ReturnFieldValue);
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }
            return (rowsAffected == 1);
        }

        public bool Del_One_ExchangeTheme_From_EXID(string EXID,string UID)
        { 
            int rowsAffected;

            SqlParameter[]
                parameters = {
								 new SqlParameter("@EXID",SqlDbType.Int),
                                 new SqlParameter("@UID",SqlDbType.UniqueIdentifier)
			};

            parameters[0].Value = EXID;
            parameters[1].Value = new Guid(UID);

            try
            {
                RunProcedure("ET_Del_One_ExchangeTheme_From_EXID", parameters, out rowsAffected);
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }
            return (rowsAffected == 1);
        }

        public bool Modify_One_ExchangeTheme_Info
            (
            string EXID,
            string UID,
            string Title,
            string Content
            )
        {
            int rowsAffected;

            SqlParameter[]
                parameters = {
								 new SqlParameter("@EXID",SqlDbType.Int),
                                  new SqlParameter("@UID",SqlDbType.UniqueIdentifier),
								 new SqlParameter("@title",SqlDbType.VarChar,500), 
								 new SqlParameter("@content",SqlDbType.VarChar,-1)  //MAX类型的对应处理方法
			};

            parameters[0].Value = EXID;
            parameters[1].Value = new Guid(UID);
            parameters[2].Value = Title;
            parameters[3].Value = Content;

            try
            {
                RunProcedure("ET_Modify_One_ExchangeTheme_Info", parameters, out rowsAffected);
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }
            return (rowsAffected == 1);
        }

        //得到一个交流主题的内容
        public DataSet Get_One_ExchangeTheme_Info_From_EXID(string EXID)
        {
            SqlParameter[]
                parameters = {
								 new SqlParameter("@EXID", SqlDbType.Int)
							 };

            parameters[0].Value = EXID;

            using (DataSet ds = RunProcedure("ET_Search_ExchangeTheme_List", parameters, "OneExchangeThemeInfo"))
            {
                return ds;
            }
        }

        //得到某个家庭发表的所有交流主题标题列表
        public DataSet Get_One_Family_All_ExchangeTheme_List
            (
            string UID,
            string Title,

            string TableName,

            int PageIndex,
            int PageSize,
            out int PageCount,
            out int TotalCount
            )
        {
            SqlParameter[]
                parameters = {
								 new SqlParameter("@UID", SqlDbType.UniqueIdentifier),
								 new SqlParameter("@Title", SqlDbType.VarChar,500),

								 new SqlParameter("@pageindex", SqlDbType.Int),
								 new SqlParameter("@pagesize", SqlDbType.Int)
							 };

            parameters[0].Value = new Guid(UID);
            parameters[1].Value = Title;
            parameters[2].Value = PageIndex;
            parameters[3].Value = PageSize;

            using (DataSet ds = RunProcedure("ET_Search_ExchangeTheme_List", parameters, TableName, out PageCount, out TotalCount))
            {
                return ds;
            }
        }

        //一般用户或者管理员获取交流主题的标题列表
        public DataSet Get_ExchangeTheme_List
            (
            string Title,
            string IsAll,

            string TableName,

            int PageIndex,
            int PageSize,
            out int PageCount,
            out int TotalCount
            )
        {
            SqlParameter[]
                parameters = {
								 new SqlParameter("@Title", SqlDbType.VarChar,500),
								 new SqlParameter("@IsAll", SqlDbType.VarChar,4),

								 new SqlParameter("@pageindex", SqlDbType.Int),
								 new SqlParameter("@pagesize", SqlDbType.Int)
							 };

            parameters[0].Value = Title;
            parameters[1].Value = IsAll;        //指示存储过程获取主题信息(part or all)
            parameters[2].Value = PageIndex;
            parameters[3].Value = PageSize;

            using (DataSet ds = RunProcedure("ET_Search_ExchangeTheme_List", parameters, TableName, out PageCount, out TotalCount))
            {
                return ds;
            }
        }

        //屏蔽或显示一条交流主题
        public bool ShieldORDisp_One_ExchangeTheme_By_EXID
        (
            string EXID,
            out int ReturnValue,
            out string ReturnFieldValue
            )
        {
            int rowsAffected;

            SqlParameter[]
                parameters = {
								 new SqlParameter("@EXID",SqlDbType.Int)
			};

            parameters[0].Value = EXID;

            try
            {
                ReturnValue = RunProcedure("ET_ShieldORDisp_One_ExchangeTheme_By_EXID", parameters, out rowsAffected, out ReturnFieldValue);
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }
            return (rowsAffected == 1);
        }


        //置顶一条交流主题
        public bool StickORCannel_One_ExchangeTheme_By_EXID
        (
            string EXID,
            out int ReturnValue,
            out string ReturnFieldValue
            )
        {
            int rowsAffected;

            SqlParameter[]
                parameters = {
								 new SqlParameter("@EXID",SqlDbType.Int)
			};

            parameters[0].Value = EXID;

            try
            {
                ReturnValue = RunProcedure("ET_StickORCannel_One_ExchangeTheme_By_EXID", parameters, out rowsAffected, out ReturnFieldValue);
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }
            return (rowsAffected == 1);
        }



    }
}
